package xuegao.study.mall.rbac.service.impl;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import xuegao.study.mall.common.domain.RoleResource;
import xuegao.study.mall.common.model.ums.*;
import xuegao.study.mall.rbac.service.UmsRoleService;
import xuegao.study.mall.repo.dao.ums.UmsMenuDao;
import xuegao.study.mall.repo.dao.ums.UmsResourceDao;
import xuegao.study.mall.repo.dao.ums.UmsRoleDao;

import javax.annotation.Resource;
import java.util.*;

@Service
public class UmsRoleServiceImpl implements UmsRoleService {

    @Autowired
    private UmsRoleDao umsRoleDao;

    @Autowired
    private UmsMenuDao umsMenuDao;

    @Autowired
    private UmsResourceDao umsResourceDao;

    @Override
    public List<UmsRole> getRolesListByAdminId(Long adminId) {
        return umsRoleDao.selectByAdminId(adminId);
    }

    @Override
    public Map<String, List<String>> initResourceRolesMap() {
        List<RoleResource> allRoleResource = umsRoleDao.getAllRoleResource();
        Map<String, List<String>> resultMap = new HashMap<>();
        if (allRoleResource != null && allRoleResource.size() > 0) {
            for (RoleResource roleResource : allRoleResource) {
                List<UmsResource> resourceList = roleResource.getResourceList();
                if (resourceList != null && resourceList.size() > 0) {
                    for (UmsResource resource : resourceList) {
                        String url = resource.getUrl();
                        List<String> roleNames = resultMap.computeIfAbsent(url, k -> new ArrayList<>());
                        roleNames.add(roleResource.getId() + "_" + roleResource.getName());
                    }
                }
            }
        }
        return resultMap;
    }

    @Override
    public List<UmsRole> selectList(Integer pageNum, Integer pageSize, String name) {
        Map<String, Object> params = new HashMap<>();
        if (StringUtils.hasText(name)) {
            params.put("name", name);
        }
        PageHelper.startPage(pageNum, pageSize);
        return umsRoleDao.selectList(params);
    }

    @Override
    public List<UmsRole> listAll() {
        return umsRoleDao.selectList(new HashMap<>());
    }

    @Override
    @Transactional
    public int insertRole(UmsRole role) {
        role.setCreateTime(new Date());
        role.setAdminCount(0);
        return umsRoleDao.insertRole(role);
    }

    @Override
    @Transactional
    public int updateRole(Long id, UmsRole role) {
        role.setId(id);
        return umsRoleDao.updateRole(role);
    }

    @Override
    @Transactional
    public int deleteById(Long id) {
        return umsRoleDao.deleteById(id);
    }

    @Override
    @Transactional
    public int allocMenu(Long roleId, List<Long> menuIds) {
        // 1.删除原有的关系
        int count = umsRoleDao.deleteMenuRoleRelationByRoleId(roleId);
        // 2.插入新的关系
        if (!CollectionUtils.isEmpty(menuIds)) {
            List<UmsRoleMenuRelation> saveList = new ArrayList<>();
            for (Long menuId : menuIds) {
                UmsRoleMenuRelation relation = new UmsRoleMenuRelation();
                relation.setRoleId(roleId);
                relation.setMenuId(menuId);
                saveList.add(relation);
            }
            return umsRoleDao.batchSaveMenuRoleRelation(saveList);
        }
        return count;
    }

    @Override
    @Transactional
    public int allocResource(Long roleId, List<Long> resourceIds) {
        // 1.删除原有关系
        int count = umsRoleDao.deleteResourceRoleRelationByRoleId(roleId);
        // 2.插入新的关系
        if (!CollectionUtils.isEmpty(resourceIds)) {
            List<UmsRoleResourceRelation> saveList = new ArrayList<>();
            for(Long resourceId : resourceIds) {
                UmsRoleResourceRelation relation = new UmsRoleResourceRelation();
                relation.setRoleId(roleId);
                relation.setResourceId(resourceId);
                saveList.add(relation);
            }
            return umsRoleDao.batchSaveResourceRoleRelation(saveList);
        }
        return count;
    }

    @Override
    public List<UmsMenu> listMenuByRoleId(Long roleId) {
        return umsMenuDao.getMenuListByRoleId(roleId);
    }

    @Override
    public List<UmsResource> listResourceByRoleId(Long roleId) {
        return umsResourceDao.getResourceListByRoleId(roleId);
    }
}
